package com.atguigu.study.controller;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.output.TokenUsage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Marco
 * @Description
 * @date 2025/8/17 22:19
 * @email 3293336923@qq.com
 */
@Slf4j
@RestController
public class LowApiController {

    @Autowired
    private ChatModel  qWen;

    @Autowired
    private ChatModel  deepSeek;

    @GetMapping("/marco/api01")
    public    String     api01(@RequestParam(name = "question" , defaultValue = "你是谁") String   question){

        String result = qWen.chat(question);
        log.info("result:{}" , result);

        return  result;
    }


    /***
     *  计算 token 用量
     * @param question
     * @return
     */
    @GetMapping("/marco/api02")
    public    String     api02(@RequestParam(name = "question" , defaultValue = "你是谁") String   question){


//        1、根据 用户输入的 提问 获取 一个 AI 大模型 返回一个 响应对象
        ChatResponse chatResponse = deepSeek.chat(UserMessage.from(question));

//       2、从 响应对象中获取 大模型返回的结果
        String content = chatResponse.aiMessage().text();

//        3、获取  大模型 的 用量
        TokenUsage tokenUsage = chatResponse.tokenUsage();

        log.info("content:{}" , content);
        log.info("tokenUsage:{}" , tokenUsage);
        return  content;
    }

}
